package com.weblog.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weblog.weblog.common.domain.dos.ArticleDO;
import com.weblog.weblog.common.domain.dos.ArticlePublishCountDO;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * @author gc
 * @date 2023年10月13日 13:47
 */

public interface ArticleMapper extends BaseMapper<ArticleDO> {


    /*
     * 分页查询
     * @author gc
     * @date 2024/1/24 18:27
     * @param current
     * @param size
     * @param title
     * @param startDate
     * @param endDate
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.weblog.weblog.common.domain.dos.ArticleDO>
     */
    default Page<ArticleDO> selectPageList(Long current, Long size, String title,
                                           LocalDate startDate, LocalDate endDate, Integer type){
        //分页对象
        Page<ArticleDO> page = new Page<>(current,size);

        //查询条件
        LambdaQueryWrapper<ArticleDO> wrapper = Wrappers.<ArticleDO> lambdaQuery()
                .like(StringUtils.isNotBlank(title),ArticleDO::getTitle,title)
                .ge(Objects.nonNull(startDate),ArticleDO::getCreateTime,startDate)
                .le(Objects.nonNull(endDate),ArticleDO::getCreateTime,endDate)
                .eq(Objects.nonNull(type), ArticleDO::getType, type)
                .orderByDesc(ArticleDO::getWeight)
                .orderByDesc(ArticleDO::getCreateTime);
        return selectPage(page,wrapper);
    }

    /*
     * 根据文章 ID 批量分页查询
     * @author gc
     * @date 2023/10/31 14:41
     * @param current
     * @param size
     * @param articleIds
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.weblog.weblog.common.domain.dos.ArticleDO>
     */
    default Page<ArticleDO> selectPageListByArticleIds(Long current, Long size, List<Long> articleIds) {
        // 分页对象(查询第几页、每页多少数据)
        Page<ArticleDO> page = new Page<>(current, size);

        // 构建查询条件
        LambdaQueryWrapper<ArticleDO> wrapper = Wrappers.<ArticleDO>lambdaQuery()
                .in(ArticleDO::getId, articleIds) // 批量查询
                .orderByDesc(ArticleDO::getCreateTime); // 按创建时间倒叙

        return selectPage(page, wrapper);
    }

    /*
     * 查询上一篇文章
     * @author gc
     * @date 2023/11/2 16:03
     * @param articleId
     * @return com.weblog.weblog.common.domain.dos.ArticleDO
     */
    default ArticleDO selectPreArticle(Long articleId){
        return selectOne(Wrappers.<ArticleDO> lambdaQuery()
                .orderByAsc(ArticleDO::getId)
                .gt(ArticleDO::getId , articleId)
                .last("limit 1"));
    }

    /*
     * 查询下一篇文章
     * @author gc
     * @date 2023/11/2 16:06
     * @param articleId
     * @return com.weblog.weblog.common.domain.dos.ArticleDO
     */
    default ArticleDO selectNextArticle(Long articleId){
        return selectOne(Wrappers.<ArticleDO>lambdaQuery()
                .orderByDesc(ArticleDO::getId)
                .lt(ArticleDO::getId , articleId)
                .last("limit 1"));
    }

    /**
     * 阅读量 + 1
     * @author gc
     * @date 2023/11/9 17:07
     * @return int
     */
    default int increaseReadNum(Long articleId){
        return update(null,Wrappers.<ArticleDO>lambdaUpdate()
                .setSql("read_num = read_num + 1")
                .eq(ArticleDO::getId , articleId));
    }
    /**
     * 查询所有记录阅读数
     * @author gc
     * @date 2023/11/11 20:09
     * @return java.util.List<com.weblog.weblog.common.domain.dos.ArticleDO>
     */
    default List<ArticleDO> selectAllReadNum(){

        return selectList(Wrappers.<ArticleDO> lambdaQuery()
                .select(ArticleDO::getReadNum));
    }

    /**
     * 按日分组，统计每日发布文章数量
     * @author gc
     * @date 2023/11/11 20:33
     * @return java.util.List<com.weblog.weblog.common.domain.dos.ArticlePublishCountDO>
     */
    @Select("SELECT DATE(create_time) AS date, COUNT(*) AS count\n" +
            "FROM t_article\n" +
            "WHERE create_time >= #{startDate} AND create_time <= #{endDate}\n" +
            "GROUP BY DATE(create_time)")
    List<ArticlePublishCountDO> selectDateArticlePublishCount(LocalDate startDate , LocalDate endDate);

    /*
     * 查询最大权重值记录
     * @author gc
     * @date 2024/1/24 17:49
     * @return com.weblog.weblog.common.domain.dos.ArticleDO
     */
    default ArticleDO selectMaxWeight(){
        return selectOne(Wrappers.<ArticleDO>lambdaQuery()
                .orderByDesc(ArticleDO::getWeight)
                .last("LIMIT 1"));
    }

    /**
     * 批量更新文章
     * @param articleDO
     * @param ids
     * @return
     */
    default int updateByIds(ArticleDO articleDO,List<Long> ids){
        return update(articleDO, Wrappers.<ArticleDO>lambdaUpdate()
                .in(ArticleDO::getId,ids));
    }
}
